package com.netclass.meethealth.srv.impl;

import android.text.TextUtils;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.netclass.meethealth.callback.ApplyCallBack;
import com.netclass.meethealth.callback.BoolCallBack;
import com.netclass.meethealth.callback.SportCallBack;
import com.netclass.meethealth.model.Apply;
import com.netclass.meethealth.model.Sport;
import com.netclass.meethealth.net.XClient;
import com.netclass.meethealth.srv.SportService;
import com.netclass.meethealth.tools.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodong on 15/11/22.
 * <p/>
 * update 15/12/6
 */
public class SportSrvImpl implements SportService {

    private final static String TAG = "SportService";

    @Override
    public void publishSport(String intro, String place, int delay, int limit, String name, String phone, double plat, double plon, double ulat, double ulon, final BoolCallBack bcb) {
        Map<String, String> params = new HashMap<>();
        params.put("intro", intro);
        params.put("name", name);
        params.put("phone", phone);
        params.put("place", place);
        params.put("plat", plat + "");
        params.put("plon", plon + "");
        params.put("lat", ulat + "");
        params.put("lon", ulon + "");
        params.put("limit", limit + "");
        params.put("delay", delay + "");

        XClient.getInstance().doPost(XClient.baseUrl + "newSport", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    bcb.onNo();
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 200) {   //没有数据返回
                    bcb.onYes();
                    return;
                } else {
                    bcb.onNo();
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                bcb.onNo();
                return;
            }
        });
    }

    @Override
    public void deleteSport(int sid, String phone, final BoolCallBack bcb) {
        Map<String, String> params = new HashMap<>();
        params.put("id", sid + "");

        XClient.getInstance().doPost(XClient.baseUrl + "delSport", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    bcb.onNo();
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 200) {   //没有数据返回
                    bcb.onYes();
                    return;
                } else {
                    bcb.onNo();
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                bcb.onNo();
                return;
            }
        });
    }

    @Override
    public void viewApplys(int sid, String phone, final ApplyCallBack acb) {
        Map<String, String> params = new HashMap<>();
        params.put("s_id", sid + "");
        params.put("phone", phone);

        XClient.getInstance().doPost(XClient.baseUrl + "listApplys", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    acb.onFailed("获取数据失败");
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 201) {   //有数据返回
                    JSONArray jsonArray = JsonUtils.getArray(JsonUtils.getData(result));
                    if (jsonArray == null) {
                        acb.onFailed("解析数据失败");
                        return;
                    }
                    List<Apply> applys = new ArrayList<Apply>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = jsonArray.getJSONObject(i);
                            applys.add(JsonUtils.fromJson(jsonObject.toString(), Apply.class));
                        } catch (JSONException e) {
                            e.printStackTrace();
                            acb.onFailed("解析数据失败");
                            return;
                        }
                    }
                    if (applys.size() > 1)
                        acb.onGetList(applys);
                    else
                        acb.onSuccess(applys.get(0));
                    return;
                } else if (ret == 200) {   //没有数据返回
                    acb.onNull();
                    return;
                } else {
                    acb.onFailed("请求失败");
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                acb.onFailed(s);
                return;
            }
        });
    }

    @Override
    public void listSport(int page, String phone, double lat, double lon, final SportCallBack spb) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        params.put("phone", phone + "");
        params.put("lat", lat + "");
        params.put("lon", lon + "");

        XClient.getInstance().doPost(XClient.baseUrl + "listSports", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    spb.onFailed("获取数据失败");
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 201) {   //有数据返回
                    JSONArray jsonArray = JsonUtils.getArray(JsonUtils.getData(result));
                    if (jsonArray == null) {
                        spb.onFailed("解析数据失败");
                        return;
                    }
                    List<Sport> sports = new ArrayList<Sport>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = jsonArray.getJSONObject(i);
                            sports.add(JsonUtils.fromJson(jsonObject.toString(), Sport.class));
                        } catch (JSONException e) {
                            e.printStackTrace();
                            spb.onFailed("解析数据失败");
                            return;
                        }
                    }
                    spb.onGetList(sports);
                    return;
                } else if (ret == 200) {   //没有数据返回
                    spb.onNull();
                    return;
                } else {
                    spb.onFailed("请求失败");
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                spb.onFailed(s);
                return;
            }
        });
    }

    @Override
    public void applySport(int sid, String name, String phone, final BoolCallBack bcb) {
        Map<String, String> params = new HashMap<>();
        params.put("s_id", sid + "");
        params.put("name", name);
        params.put("phone", phone);

        XClient.getInstance().doPost(XClient.baseUrl + "apply", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    bcb.onNo();
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 200) {   //没有数据返回
                    bcb.onYes();
                    return;
                } else {
                    bcb.onNo();
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                bcb.onNo();
                return;
            }
        });
    }

    @Override
    public void updateApplyInfo(int aid, String name, String phone, final BoolCallBack bcb) {
        Map<String, String> params = new HashMap<>();
        params.put("id", aid + "");
        params.put("name", name);
        params.put("phone", phone);

        XClient.getInstance().doPost(XClient.baseUrl + "updateApply", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    bcb.onNo();
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 200) {   //没有数据返回
                    bcb.onYes();
                    return;
                } else {
                    bcb.onNo();
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                bcb.onNo();
                return;
            }
        });
    }

    @Override
    public void deleteApply(int aid, String phone, final BoolCallBack bcb) {
        Map<String, String> params = new HashMap<>();
        params.put("id", aid + "");
        params.put("phone",phone);

        XClient.getInstance().doPost(XClient.baseUrl + "delApply", params, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result.toString();
                Log.i(TAG, result);
                if (TextUtils.isEmpty(result)) {
                    bcb.onNo();
                    return;
                }
                int ret = JsonUtils.getRet(result);
                if (ret == 200) {   //没有数据返回
                    bcb.onYes();
                    return;
                } else {
                    bcb.onNo();
                    return;
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                bcb.onNo();
                return;
            }
        });
    }

}
